﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateVote : WebPart
    {
       int MaxItems = 10;
       public override string WebPartId
        {
            get
            {
                return "UpdateVote";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Ảnh quảng cáo");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.UpdateVote));
            string VoteId = System.Web.HttpContext.Current.Request["VoteId"];

            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "Vote.js") +
                // Portal.Environment.WebPartUtilities.AttachStyleSheet("Vote", "StyleSheet.css") +
                // "<div class=cssForm>" +
                //"<div class=cssFormTitle>" + ResourceBLL.GetText(WebPartId,"UpdateVoteFormTitle","Cập nhật ảnh quảng cáo") + "</div>" +
                 Portal.Environment.Environments.GetFormDialog(
                 "<div class=cssFormContent id=divFormContent>" +
                    GetHtml(Environments.ActiveLanguage, VoteId) +
                 "</div>", ResourceBLL.GetText(WebPartId, "UpdateVoteFormTitle", "Cập nhật ảnh quảng cáo"));
                 //"</div>";

            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string ActiveLanguage,string VoteId)
        {
            string RetHtml = "";


            string SelectLanguageText =
              "<select id=divSelectLanguage onchange=javascript:DetectLanguageBody('" + VoteId + "');>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";


            DataTable dtVoteItems = new VoteBLL().ReadingVoteItems(VoteId);
            int TotalRow = dtVoteItems.Rows.Count;

            VoteCls VoteCls = new VoteBLL().CreateModel(ActiveLanguage, VoteId);
            if (VoteCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }
            RetHtml +=
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateVoteLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateVoteCaption", "Thăm dò ý kiến") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtVote value=\"" + VoteCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateVote('" + VoteId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateVoteSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateVoteCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>";



            RetHtml += "      <div class=cssVoteItems>" +
                             "<table>" +
                             "<tr>" +
                             "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemName","Tên đối tượng") + "</th>" +
                             "      <th>" + ResourceBLL.GetText(WebPartId,"LinkItemIndexOrder","Vị trí") + "</th>" +
                             "</tr>";
            for (int iIndex = 0; iIndex < dtVoteItems.Rows.Count; iIndex++)
            {
                VoteItemCls VoteItemCls = new VoteItemCls();
                VoteItemCls.VoteItemName = "";
                try
                {
                    VoteItemCls.VoteItemName = (string)dtVoteItems.Rows[iIndex][ActiveLanguage + "VoteItemName"];
                }
                catch { }
                VoteItemCls.VoteItemId = (string)dtVoteItems.Rows[iIndex]["VoteItemId"];
                VoteItemCls.IndexOrder = (int)dtVoteItems.Rows[iIndex]["IndexOrder"];
                RetHtml += ServerSideNewLinkItem(iIndex, VoteItemCls);
            }
            for (int iIndex = dtVoteItems.Rows.Count; iIndex < MaxItems; iIndex++)
            {
                RetHtml += ServerSideNewLinkItem(iIndex, null);
            }
            RetHtml += "</table>" +
                     "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideNewLinkItem(int Index, VoteItemCls VoteItemCls)
        {
            string Html = "";
            if (VoteItemCls != null)
            {
                Html = "<tr>" +
                       "    <td class=cssCol1><input class=cssTextBoxDialog class=cssInputItemName value=\"" + VoteItemCls.VoteItemName + "\" id=\"txtItemName_" + Index + "\"></td>\r\n" +
                       "    <td class=cssCol4><input type=hidden id=txtVoteItemId_"+Index+" value=\""+VoteItemCls.VoteItemId+"\"><input value=\"" + VoteItemCls.IndexOrder.ToString() + "\" class=cssInputItemIndexOrder value=\"" + (Index + 1) + "\" id=\"txtIndexOrder_" + Index + "\"></td>\r\n" +
                       "</tr>";
            }
            else
            {
                Html = "<tr>" +
                      "    <td class=cssCol1><input class=cssTextBoxDialog class=cssInputItemName value=\"\" id=\"txtItemName_" + Index + "\"></td>\r\n" +
                      "    <td class=cssCol4><input value=\"" + (Index + 1) + "\" class=cssInputItemIndexOrder value=\"" + (Index + 1) + "\" id=\"txtIndexOrder_" + Index + "\"></td>\r\n" +
                      "</tr>";
            }
            return Html;
        }


        [AjaxPro.AjaxMethod]
        public VoteItemCls[] GetVoteItemsObject()
        {
            VoteItemCls[] VoteItems = new VoteItemCls[MaxItems];
            for (int iIndex = 0; iIndex < VoteItems.Length; iIndex++)
            {
                VoteItems[iIndex] = new VoteItemCls();
            }
            return VoteItems;
        }

        [AjaxPro.AjaxMethod]
        public string GetShowAppendForm(string VoteId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddVote().WebPartId),
                        new ParamCls("VoteId",VoteId)
                    });
            return AppendUrl;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateVote(string ActiveLanguage,string VoteId, string VoteName, VoteItemCls[] VoteItems)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (VoteName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên đối tượng"));
                new VoteBLL().UpdateVote(null, ActiveLanguage,VoteId, VoteName, VoteItems);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetQuestion(string ActiveLanguage,string VoteId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                return new VoteBLL().CreateModel(ActiveLanguage, VoteId).Subject;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}